home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / os2tools / bnklysrc / b_sessio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-25  |  17.3 KB  |  487 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  Copyright (C) 1987, 1988, 1989 by Robert Hartman and Vincent Perriello  */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                This module was written by Vince Perriello                */
  14. /*           (based heavily on a similar work by Wynn Wagner III)           */
  15. /*                with modifications done by Bob Hartman                    */
  16. /*                                                                          */
  17. /*                                                                          */
  18. /*                   BinkleyTerm Mail Session Initiator                     */
  19. /*                                                                          */
  20. /*                                                                          */
  21. /*    For complete  details  of the licensing restrictions, please refer    */
  22. /*    to the License  agreement,  which  is published in its entirety in    */
  23. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.210.    */
  24. /*                                                                          */
  25. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  26. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  27. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  28. /*    NOT HAVE THESE FILES,  YOU SHOULD  IMMEDIATELY CONTACT THE AUTHORS    */
  29. /*    AT THE  ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO    */
  30. /*    USE   THIS  FILE  WITHOUT  HAVING   ACCEPTED  THE  TERMS  OF   THE    */
  31. /*    BINKLEYTERM  LICENSING AGREEMENT,  OR SUCH OTHER  AGREEMENT AS YOU    */
  32. /*    ARE ABLE TO REACH WITH THE AUTHORS.                                   */
  33. /*                                                                          */
  34. /*                                                                          */
  35. /*    The Authors can be reached at the following addresses:                */
  36. /*                                                                          */
  37. /*    Robert C. Hartman                      Vincent E. Perriello           */
  38. /*    Spark Software                         VEP Software                   */
  39. /*    427-3 Amherst Street                   111 Carroll Street             */
  40. /*    CS2032, Suite 232                      Naugatuck, CT 06770            */
  41. /*    Nashua, NH 03061                                                      */
  42. /*                                                                          */
  43. /*    FidoNet 1:132/101                      FidoNet 1:141/491              */
  44. /*    Data    (603) 888-8179                 Data    (203) 729-7569         */
  45. /*                                                                          */
  46. /*    Please feel free to contact us at any time to share your comments     */
  47. /*    about our software and/or licensing policies.                         */
  48. /*                                                                          */
  49. /*--------------------------------------------------------------------------*/
  50.  
  51. #include <stdio.h>
  52. #include <signal.h>
  53. #include <ctype.h>
  54. #include <conio.h>
  55. #include <string.h>
  56.  
  57. #define WAZOO_SECTION
  58. #define MATRIX_SECTION
  59.  
  60. #include "com.h"
  61. #include "xfer.h"
  62. #include "zmodem.h"
  63. #include "keybd.h"
  64. #include "sbuf.h"
  65. #include "sched.h"
  66. #include "externs.h"
  67. #include "prototyp.h"
  68.  
  69. void b_session (was_initiator)
  70. int was_initiator;
  71. {
  72.    long t1, t2, t3;
  73.    unsigned char j, d;
  74.    int k, i;
  75.    char *c, *m[16];
  76.    char buff[128];
  77.    int got_a_char = 0;                           /* For 'dead silence'
  78.                                                   * timeout */
  79.  
  80.    mail_finished = 0;
  81.  
  82.    if (un_attended && fullscreen)
  83.       {
  84.       do_ready ("Connect");
  85.       }
  86.  
  87.    /* Remove the old .REQ and .RSP files */
  88.    for (i = 0; i < 10; i++)
  89.       {
  90.       if (alias[i].Net == 0)
  91.          break;
  92.       sprintf (buff, "%s%04x%04x.REQ", CurrentNetFiles, alias[i].Net, alias[i].Node);
  93.       unlink (buff);
  94.  
  95.       sprintf (buff, "%04x%04x.RSP", alias[i].Net, alias[i].Node);
  96.       unlink (buff);
  97.       }
  98.  
  99.    remote_capabilities = 0;
  100.    if (was_initiator)
  101.       goto initiate_outbound;
  102. /*
  103.  *    INBOUND -- meaning we were connected to by another system.
  104.  */
  105.    /* First a five second delay, or a char is found */
  106.    t2 = timerset (500);
  107.  
  108.    t1 = timerset (1000);                         /* 10 secs silence T.O. */
  109.    i = 0;
  110.  
  111.    /* As long as we have carrier, and 5 seconds is not up, and no */
  112.    /* special chars have been received, just continue looping */
  113.  
  114.    while (CARRIER && (!timeup (t2)) && (i == 0))
  115.       {
  116.       switch (i = (unsigned char) (PEEKBYTE () & 0x7f))
  117.          {
  118.          case 0x7f:                              /* No char available    */
  119.             i = 0;                               /* i = 0 keeps us here  */
  120.             break;
  121.  
  122.          case CR:
  123.          case LV:
  124.          case ' ':
  125.          case TSYNC & 0x7f:
  126.          case YOOHOO & 0x7f:
  127.          case ENQ:
  128.          case ESC:
  129.             break;                               /* Magic characters     */
  130.  
  131.          default:                                /* Non-magic character  */
  132.             ++got_a_char;                        /* Say we saw something */
  133.             TIMED_READ (0);                      /* Then eat it up       */
  134.             i = 0;                               /* And stay in the loop */
  135.          }
  136.       }
  137.  
  138.    sprintf (buff, "\r\r* Network Address %d:%d/%d.%d Using %s\r\n",
  139.             alias[0].Zone, boss_net, boss_node,
  140.           (boss_net == alias[0].Net) ? 0 : alias[0].Node, ANNOUNCE);
  141.  
  142.    sendbanner (buff);
  143.  
  144.    t2 = timerset (BBStimeout);                   /* 20 second timeout    */
  145.  
  146.    if (strlen (BBSbanner) != 0)                  /* If BBS name spec'ed, */
  147.       {
  148.       SENDBYTE ('\r');                           /* make sure user gets  */
  149.       SENDBYTE ('\n');                           /* a full cr/lf...      */
  150.  
  151.       sendbanner (BBSbanner);
  152.  
  153.       SENDBYTE ('\r');                           /* make sure user gets  */
  154.       SENDBYTE ('\n');                           /* a full cr/lf...      */
  155.       }
  156.  
  157.    if (!mail_only)
  158.       {
  159.       sendbanner (BBSwelcome);
  160.       }
  161.  
  162. play_banner:
  163.    if (mail_only)                                /* If no BBS allowed,   */
  164.       c = noBBS;                                 /* tell human to git    */
  165.    else c = BBSesc;                              /* or hit ESC for BBS   */
  166.  
  167.    sendbanner (c);
  168.  
  169. get_handshake:
  170.    for (k = 0; k < num_ext_mail; k++)
  171.       {
  172.       m[k] = ext_mail_string[k];                 /* UUCP handshake       */
  173.       }
  174.  
  175.    while (CARRIER && (!timeup (t2)))
  176.       {
  177.  
  178.       if (got_ESC ())                            /* Manual abort?        */
  179.          {
  180.          DTR_OFF ();                             /* Yes, drop carrier    */
  181.          timer (10);
  182.          return;
  183.          }
  184.  
  185.       i = PEEKBYTE () & 0xff;                    /* Get the next char    */
  186.  
  187.       switch (i)
  188.          {
  189.          case 0xff:                              /* No character there   */
  190.             if (!got_a_char && timeup (t1) && !mail_only)
  191.                goto spawnBBS;                    /* 10 secs silence = human! */
  192.             continue;
  193.  
  194.          case YOOHOO:                            /* Looks like an Opus   */
  195. got_a_yoohoo:
  196.  
  197.             ++got_a_char;                        /* Note that we got this */
  198.  
  199.             if (no_WaZOO)                        /* If we're not WaZOO,   */
  200.                {
  201.                i = TIMED_READ (0);               /* Eat the YooHoo        */
  202.            continue;
  203.            }
  204.  
  205.             CLEAR_OUTBOUND ();                   /* End noisy banner now  */
  206.  
  207.             if (YooHoo_Receiver ())              /* Get its HELLO string */
  208.                {
  209.                last_type (1, remote_zone, remote_net, remote_node);
  210.                ++hist.mail_calls;
  211.                sb_move (historywin, HIST_MAIL_ROW, HIST_COL);
  212.                sprintf (junk, "%d/%d", hist.bbs_calls, hist.mail_calls);
  213.                sb_puts (historywin, junk);
  214.  
  215.                WaZOO (0);                        /* And do WaZOO!!!      */
  216.                return;
  217.                }
  218.             else if (!hup)
  219.                goto get_handshake;               /* bad hello, try again */
  220.             else return;
  221.  
  222.          case TSYNC:                            /* Looks like an FTSC   */
  223.             /* See if we get anything within 2 seconds, if not, then it */
  224.             /* is FTSC.  If we get another TSYNC, it also means FTSC.   */
  225.             /* BUT, if we get a YOOHOO, then we can do WaZOO, and it    */
  226.             /* just means that we missed the first YOOHOO - lucky us.   */
  227.  
  228.             CLEAR_INBOUND ();
  229.             CLEAR_OUTBOUND ();                   /* End noisy banner now  */
  230.             if (no_WaZOO)                        /* If we're not WaZOO,   */
  231.                goto do_FTSC;                     /* Don't bother waiting  */
  232.  
  233.             t3 = timerset (200);                 /* Set for 2 seconds */
  234.             d = 0;
  235.             while (CARRIER && (!timeup (t3)) && (!d))
  236.                {
  237.                if ((i = PEEKBYTE ()) >= 0)
  238.                   {
  239.                   switch (i)
  240.                      {
  241.                      case YOOHOO:
  242.                         goto got_a_yoohoo;
  243.  
  244.                      case TSYNC:
  245.                         d = 1;
  246.                         break;
  247.  
  248.                      default:
  249.                         TIMED_READ (0);
  250.                      }
  251.                   }
  252.                }
  253. do_FTSC:
  254.             if (CARRIER)
  255.                {
  256.                last_type (2, -1000, 0, 0);
  257.                ++hist.mail_calls;
  258.                sb_move (historywin, HIST_MAIL_ROW, HIST_COL);
  259.                sprintf (junk, "%d/%d", hist.bbs_calls, hist.mail_calls);
  260.                sb_puts (historywin, junk);
  261.  
  262.                if (un_attended && fullscreen)
  263.                   sb_show ();
  264.  
  265.                FTSC_receiver (0);
  266.                }
  267.             return;
  268.  
  269.          case CR:
  270.             i = TIMED_READ (0);                  /* Eat the CR            */
  271.             ++got_a_char;                        /* Note that we got this */
  272.             goto play_banner;                    /* Play the banner again */
  273.  
  274.          case ESC:                              /* This will be for BBS  */
  275.             if (!mail_only)                      /* If BBS operation OK,  */
  276.                goto spawnBBS;                    /* go do it...           */
  277.  
  278.          default:
  279.             ++got_a_char;                        /* Note that we got this */
  280.             i = TIMED_READ (0) & 0xff;           /* Eat the character     */
  281.             for (k = 0; k < num_ext_mail; k++)
  282.                {
  283.                if (i != *(m[k]++))               /* Does this match next? */
  284.                   m[k] = ext_mail_string[k];     /* No, reset pointer     */
  285.                else if (!*m[k])                  /* End of UUCP string?   */
  286.                   goto spawn_UUCP;               /* Yes, start UUCP       */
  287.                }
  288.          }
  289.       }
  290. spawnBBS:
  291.    if (CARRIER && !mail_only)
  292.       {
  293.       last_type (3, 0, 0, 0);
  294.  
  295.       BBSexit ();
  296.       }
  297.    return;
  298. spawn_UUCP:
  299.    if (CARRIER)
  300.       {
  301.       last_type (4, 0, 0, 0);
  302.       UUCPexit (lev_ext_mail[k]);
  303.       }
  304.    return;
  305.  
  306. /*
  307.  *    OUTBOUND -- meaning call was made by us.
  308.  */
  309.  
  310. initiate_outbound:
  311.  
  312.    last_type (1, remote_zone, remote_net, remote_node);
  313.    if (Whack_CR ())
  314.       {
  315.       return;
  316.       }
  317.    t1 = timerset (3000);                         /* set 30 second timeout */
  318.    j = 'j';                                      /* Something that will not
  319.                                                   * trip the states below */
  320.    t3 = timerset (50);                           /* Wait for .5 second of
  321.                                                   * clear line */
  322.    while (!timeup (t1) && CARRIER)               /* till then or CD lost  */
  323.       {
  324.       if (!no_WaZOO && !no_WaZOO_Session)        /* If we're WaZOO,   */
  325.          SENDBYTE (YOOHOO);                      /* try YooHoo?       */
  326.       SENDBYTE (TSYNC);                          /* Always try FSC    */
  327.  
  328.       t2 = timerset (300);                       /* Time between sending
  329.                                                   * startup seq */
  330.       while (CARRIER && (!timeup (t2)))
  331.          {
  332.          i = TIMED_READ (0);
  333.  
  334.          /* Because Fido v12 has a tendency to echo the YooHoo and throw */
  335.          /* off the value of j which we use later on.  We just ignore it */
  336.          if ((i & 0x7f) == (YOOHOO & 0x7f))
  337.             continue;
  338.  
  339.          switch (i)
  340.             {
  341.             case ENQ:
  342.                if (no_WaZOO || no_WaZOO_Session) /* If we're not WaZOO,   */
  343.                   break;                         /* ignore it             */
  344.  
  345.                if (YooHoo_Sender ())             /* Send Hello packet,    */
  346.                   {
  347.                   WaZOO (1);
  348.                   return;
  349.                   }
  350.                break;
  351.  
  352.             case 0x00:
  353.             case 0x01:
  354.             case 'C':
  355.                if (j == 'C')
  356.                   {
  357.                   FTSC_sender (0);
  358.                   return;
  359.                   }
  360.  
  361.                if (timeup (t3))
  362.                   {
  363.                   timer (1);
  364.                   if (CHAR_AVAIL ())
  365.                      {
  366.                      /* If it is SEAlink stuff, do it */
  367.                      i = TIMED_READ (0);
  368.                      if ((i == 0) || (i == 1) || (i == 'C') || (i == NAK))
  369.                         {
  370.                         FTSC_sender (0);
  371.                         return;
  372.                         }
  373.  
  374.                      t3 = timerset (50);
  375.                      j = 'j';
  376.                      }
  377.                   }
  378.                break;
  379.  
  380.             case 0xfe:
  381.             case 0xfffe:
  382.                if (j == 0x01)
  383.                   {
  384.                   FTSC_sender (0);
  385.                   return;
  386.                   }
  387.                break;
  388.  
  389.             case 0xff:
  390.             case 0xffff:
  391.                if (j == 0x00)
  392.                   {
  393.                   FTSC_sender (0);
  394.                   return;
  395.                   }
  396.                break;
  397.  
  398.             case NAK:
  399.                if (j == NAK)
  400.                   {
  401.                   FTSC_sender (0);
  402.                   return;
  403.                   }
  404.                break;
  405.             }
  406.  
  407.      if ((i != (int) 0xffff) && (i != 0xff))
  408.             {
  409.             /* We got a character */
  410.             CLEAR_INBOUND ();
  411.  
  412.             if (timeup (t3))
  413.                {
  414.                /* We already saw a clear line */
  415.                j = (unsigned char) (i & 0xff);
  416.                }
  417.             else
  418.                {
  419.  
  420.                /*
  421.                 * We haven't seen a clear line yet, so reset the timer. We do
  422.                 * this so that opening screens that contain many 'C'
  423.                 * characters don't confuse us into FSC001.  FSC001 can only
  424.                 * start after .5 second of clear line is seen. 
  425.                 */
  426.                t3 = timerset (50);
  427.                }
  428.             }
  429.          }
  430.       }
  431. }
  432.  
  433. /*
  434.  * Send banner-type lines to remote. Since strange modems like Courier HST
  435.  * have a propensity for thinking they are connected when talking to a dial
  436.  * tone (but do leave CTS down just to screw us up) we have to use a timeout
  437.  * to figure out if we are in trouble, and if so, we drop DTR to make the
  438.  * bogus carrier go away.
  439.  *
  440.  */
  441.  
  442. void sendbanner (string)
  443. char *string;
  444. {
  445.    long t1;
  446.    register char c;
  447.  
  448.    t1 = timerset (600);                         /* 60 seconds max to put out
  449.                                                  * banner */
  450.  
  451.    while (!timeup (t1) && CARRIER)
  452.       {
  453.       if (got_ESC ())                           /* Manual abort?     */
  454.             break;                              /* Use failure logic */
  455.  
  456.       if (!OUT_FULL ())
  457.          {
  458.          c = *string++;
  459.          if (!c || c == 0x1a)                   /* Test for end      */
  460.             return;
  461.          SENDBYTE (c);
  462.          }
  463.       }
  464.  
  465.    /*
  466.     * If we get here we had trouble. Drop DTR now to hopefully get out of
  467.     * this trouble. Flush outbound. Pause for 1 second.
  468.     */
  469.  
  470.    DTR_OFF ();
  471.    CLEAR_OUTBOUND ();
  472.  
  473.    timer (10);
  474.  
  475.    CLEAR_INBOUND ();
  476. }
  477.  
  478.  
  479.  
  480. void b_init ()
  481. {
  482.    got_arcmail = got_packet = got_mail = sent_mail = 0;
  483.    remote_zone = remote_net = remote_node = remote_point = 0;
  484.    Netmail_Session = remote_capabilities = mail_finished = 0;
  485.    CurrentNetFiles = DEFAULT.sc_Inbound;
  486. }
  487.